<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="PULP,线性优化," />










<meta name="description" content="本文根据PuLP文档翻译而来，原文请参考https://pythonhosted.org/PuLP/main/the_optimisation_process.html#getting-the-problem-description 优化过程 解决优化问题不是一个线性过程，但过程可分为五个一般步骤：  获得问题描述  制定数学公式 处理数学公式 执行一些后优化分析 介绍解决方案和分析  但是，在此">
<meta name="keywords" content="PULP,线性优化">
<meta property="og:type" content="article">
<meta property="og:title" content="PulP线性优化（一）优化过程">
<meta property="og:url" content="http://yoursite.com/2018/08/03/pulp线性优化1/index.html">
<meta property="og:site_name" content="Frone&#39;s Blog">
<meta property="og:description" content="本文根据PuLP文档翻译而来，原文请参考https://pythonhosted.org/PuLP/main/the_optimisation_process.html#getting-the-problem-description 优化过程 解决优化问题不是一个线性过程，但过程可分为五个一般步骤：  获得问题描述  制定数学公式 处理数学公式 执行一些后优化分析 介绍解决方案和分析  但是，在此">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://pythonhosted.org/PuLP/_images/or_methodology.jpg">
<meta property="og:updated_time" content="2018-08-30T04:20:11.613Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="PulP线性优化（一）优化过程">
<meta name="twitter:description" content="本文根据PuLP文档翻译而来，原文请参考https://pythonhosted.org/PuLP/main/the_optimisation_process.html#getting-the-problem-description 优化过程 解决优化问题不是一个线性过程，但过程可分为五个一般步骤：  获得问题描述  制定数学公式 处理数学公式 执行一些后优化分析 介绍解决方案和分析  但是，在此">
<meta name="twitter:image" content="https://pythonhosted.org/PuLP/_images/or_methodology.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/08/03/pulp线性优化1/"/>





  <title>PulP线性优化（一）优化过程 | Frone's Blog</title><!-- hexo-inject:begin --><!-- hexo-inject:end -->
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Frone's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术小窝</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/03/pulp线性优化1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">PulP线性优化（一）优化过程</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-03T16:11:51+08:00">
                2018-08-03
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/08/03/pulp线性优化1/" class="leancloud_visitors" data-flag-title="PulP线性优化（一）优化过程">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><em>本文根据PuLP文档翻译而来，原文请参考</em><br><em><a href="https://pythonhosted.org/PuLP/main/the_optimisation_process.html#getting-the-problem-description" target="_blank" rel="noopener">https://pythonhosted.org/PuLP/main/the_optimisation_process.html#getting-the-problem-description</a></em></p>
<h3 id="优化过程"><a href="#优化过程" class="headerlink" title="优化过程"></a>优化过程</h3><hr>
<p>解决优化问题不是一个线性过程，但过程可分为五个一般步骤：</p>
<ul>
<li>获得问题描述 </li>
<li>制定数学公式</li>
<li>处理数学公式</li>
<li>执行一些后优化分析</li>
<li>介绍解决方案和分析</li>
</ul>
<p>但是，在此过程中通常存在“反馈循环”。例如，在制定和解决优化问题之后，您通常需要考虑解决方案的有效性（通常咨询提供问题描述的人员）。如果您的解决方案无效，您可能需要更改或更新您的配方，以包含您对实际问题的新理解。此过程显示在运筹学方法图中。<br><img src="https://pythonhosted.org/PuLP/_images/or_methodology.jpg" alt="这里写图片描述"><br>建模过程从明确定义的模型描述开始，然后使用数学方法来确定数学公式。接下来，建模者将数学公式输入到一些求解器软件中，例如Excel并求解该模型。最后，根据原始模型描述将解决方案转化为决策。</p>
<p>使用Python为您提供了建模过程的“快捷方式”。通过在Python中制定数学程序，您已经将它放入PuLP可以轻松使用的形式中，建模者可以调用许多求解器，例如CPLEX，COIN，gurobi，因此您无需将数学公式输入到求解器软件中。但是，您通常不会在配方中添加任何“硬”数字，而是使用数据文件“填充”模型，因此会涉及一些创建相应数据文件的工作。使用数据文件的优点是相同的模型可能会被不同的数据集使用多次。</p>
<h3 id="建模过程"><a href="#建模过程" class="headerlink" title="建模过程"></a>建模过程</h3><hr>
<p>建模过程是优化过程的“整洁”简化。让我们更详细地考虑优化过程的五个步骤：</p>
<h3 id="获取问题描述"><a href="#获取问题描述" class="headerlink" title="获取问题描述"></a>获取问题描述</h3><hr>
<p>此步骤的目的是提出正式，严谨的模型描述。通常，您会启动一个优化项目，其中包含问题和一些数据的抽象描述。通常，您需要花一些时间与提供问题的人（通常称为客户）交谈。通过与客户交谈并考虑可用的数据，您可以获得您习惯的更严格的模型描述。有时并非所有数据都相关，或者您需要询问客户是否可以提供其他数据。有时，可用数据的限制可能会显着改变您的模型描述和后续配方。</p>
<h3 id="制定数学公式"><a href="#制定数学公式" class="headerlink" title="制定数学公式"></a>制定数学公式</h3><hr>
<p>在此步骤中，我们从问题描述中确定关键的可量化决策，限制和目标，并在数学模型中捕获它们的依赖。我们可以将制定过程分为4个关键步骤：</p>
<ul>
<li>确定特别关注单位的决策变量（例如：我们需要确定每个进程每周运行多少小时）。</li>
<li>使用决策变量制定目标函数，我们可以构建最小化或最大化目标函数。目标函数通常反映给定决策变量值的总成本或总利润。</li>
<li>制定约束，要么是合乎逻辑的（例如，我们不能用于负数小时），要么明确指出问题描述。同样，约束以决策变量表示。</li>
<li>确定目标函数和约束所需的数据。要解决您的数学程序，您需要在目标函数和/或约束中使用一些“硬数”作为变量边界和/或变量系数。</li>
</ul>
<h3 id="解决数学程序"><a href="#解决数学程序" class="headerlink" title="解决数学程序"></a>解决数学程序</h3><hr>
<p>对于相对简单或易于理解的问题，通常可以将数学模型解决为最优性（即，识别出最佳可能的解决方案）。这是使用诸如修订的单纯形法或内点法之类的算法来完成的。然而，使用这些技术解决最优性需要很长时间的许多工业问题，因此使用不保证最优性的启发式方法来解决这些问题。</p>
<h3 id="进行一些后优化分析"><a href="#进行一些后优化分析" class="headerlink" title="进行一些后优化分析"></a>进行一些后优化分析</h3><hr>
<p>通常问题描述存在不确定性（无论是提供的数据的准确性，还是将来的数据值）。在这种情况下，我们的解决方案的稳健性可以通过执行后优化分析来检查。这涉及确定最佳解决方案在公式的各种变化下如何变化（例如，给定成本增加或特定机器失效会产生什么影响？）。这种分析对于制定战术或战略决策也很有用（例如，如果我们投资开设另一家工厂，这会对我们的收入产生什么影响？）。</p>
<p>此步骤（以及下一步）中的另一个重要考虑因素是数学程序解决方案的验证。您应该仔细考虑解决方案的变量值在原始问题描述方面的含义。确保它们对您，更重要的是您的客户有意义（这就是为什么下一步，提出解决方案和分析很重要）。</p>
<h3 id="介绍解决方案和分析"><a href="#介绍解决方案和分析" class="headerlink" title="介绍解决方案和分析"></a>介绍解决方案和分析</h3><hr>
<p>优化过程中的关键步骤是解决方案的呈现和任何后优化分析。从数学程序的解决方案转换回简明易懂的摘要与从问题描述到数学公式的转换同样重要。通过优化生成的关键观察和决策必须以易于理解的方式呈现给客户或项目利益相关者。</p>
<p>您的演示文稿是实施数学计划所产生决策的关键第一步。如果决策及其后果（通常由数学计划约束决定）没有明确而明智地呈现，那么您的最佳决策将永远不会被使用。</p>
<p>这一步也是您未来建议其他工作的机会。这可能包括：</p>
<ul>
<li>定期检查数学公式的有效性;</li>
<li>进一步分析您的解决方案，为您的客户寻找其他好处;</li>
<li>寻找未来的优化机会。</li>
</ul>

      
    </div>
    
    
    

    
      <div>
        <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/uploads/wechat-qcode.jpg" alt="Frone Xie wechat" style="width: 200px; max-width: 100%;"/>
    <div>欢迎关注我的微信公众号！</div>
</div>

      </div>
    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/PULP/" rel="tag"># PULP</a>
          
            <a href="/tags/线性优化/" rel="tag"># 线性优化</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/07/30/python生成EXCEL报表乱码问题/" rel="next" title="python生成EXCEL报表乱码问题">
                <i class="fa fa-chevron-left"></i> python生成EXCEL报表乱码问题
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/08/03/pulp线性优化2/" rel="prev" title="PulP线性优化（二）优化概念">
                PulP线性优化（二）优化概念 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Frone Xie</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">29</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">45</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/frone" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/frone" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#优化过程"><span class="nav-number">1.</span> <span class="nav-text">优化过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#建模过程"><span class="nav-number">2.</span> <span class="nav-text">建模过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取问题描述"><span class="nav-number">3.</span> <span class="nav-text">获取问题描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#制定数学公式"><span class="nav-number">4.</span> <span class="nav-text">制定数学公式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解决数学程序"><span class="nav-number">5.</span> <span class="nav-text">解决数学程序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#进行一些后优化分析"><span class="nav-number">6.</span> <span class="nav-text">进行一些后优化分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#介绍解决方案和分析"><span class="nav-number">7.</span> <span class="nav-text">介绍解决方案和分析</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Frone Xie</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">本站访客数</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人数
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">本站总访问量</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>





  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=65946112";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("ohqkXUU5txOlX3MjJPCrw7jG-gzGzoHsz", "bPQhLzRSEsTqdIKAGRcHLhyN");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "box";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

  <script type="text/javascript" src="/js/src/love.js"></script><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</body>
</html>
